package com.zb.zeus.common.security.context;

import com.zb.zeus.system.api.model.LoginUser;

import java.util.Objects;

/**
 * @author fujianjian
 * @since 2021/7/9 15:44
 */
public class UserContextHolder {
    public static final LoginUser DEFAULT_USER = new LoginUser();
    static {
        DEFAULT_USER.setUserid(0L);
        DEFAULT_USER.setUsername("UNAUTHORIZED");
        DEFAULT_USER.setAdNum("UNAUTHORIZED");
    }

    private static final ThreadLocal<LoginUser> LOGIN_USER_CONTEXT = new ThreadLocal<>();

    public static void setLoginUser(LoginUser loginUser) {
        LOGIN_USER_CONTEXT.set(loginUser);
    }

    public static LoginUser getLoginUser() {
        LoginUser loginUser = LOGIN_USER_CONTEXT.get();
        return Objects.isNull(loginUser) ? DEFAULT_USER : loginUser;
    }

    public static void remove() {
        LOGIN_USER_CONTEXT.remove();
    }

}
